using UnityEngine;
using System.Collections;
using DanielSig.Geometry;
using DanielSig.UnityExtensions;

//namespace DanielSig.Geometry
//{
	[AddComponentMenu("Mesh/Morpher")]
	
	[RequireComponent(typeof(MeshFilter))]
	
	public class Morpher : MonoBehaviour
	{
		public bool update = true;
		public bool morphOnStartUpOnly = false;
		public Vector3 direction = Vector3.up;
		public Vector3 position = Vector3.zero;
		public Vector3 rotation = Vector3.zero;
		public Vector3 scale = Vector3.zero;
		public int resolution = Geom.DEFAULT_RESOLUTION;
	
		private void Start ()
		{
			_shapeFilter = (MeshFilter)GetComponent<MeshFilter>();
			_shapeMesh = _shapeFilter.mesh;
			UpdateMesh();
			if(morphOnStartUpOnly)
			{
				_shapeFilter = null;
				_shapeMesh = null;
				Destroy(this);
			}
		}
		private void Update()
		{
			direction.Normalize();
			UpdateMesh();
		}
		private void UpdateMesh()
		{
			if(update)
			{
				_shapeFilter.mesh = _shapeMesh.GetMorphed(direction, position, Quaternion.Euler(rotation), scale, resolution);	
			}
		}
		private MeshFilter _shapeFilter;
		private Mesh _shapeMesh;
	}
//}